package com.example.basic.controller;

import com.example.basic.aop.Lock;
import com.example.basic.domain.Task;
import com.example.basic.reslut.ResponseResult;
import com.example.basic.service.TaskService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/task")
public class TaskController {

    private final TaskService taskService;

    @Autowired
    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @GetMapping("/selectById/{taskId}")
    public ResponseResult<Task> selectById(@PathVariable("taskId") Long taskId) {
        return ResponseResult.success(taskService.selectById(taskId));
    }

    @GetMapping("/selectAll")
    public ResponseResult<PageInfo<Task>> selectAll(int pageNum, int pageSize) {
        return ResponseResult.success(taskService.selectAll(pageNum, pageSize));
    }

    @PostMapping("/insert")
    public ResponseResult<Task> insert(@RequestBody Task task) {
        return ResponseResult.success(taskService.insert(task));
    }

    @PostMapping("/update")
    @Lock(keys = "#task.taskId")
    public ResponseResult<Task> update(@RequestBody Task task) throws InterruptedException {
        TimeUnit.SECONDS.sleep(300);
        return ResponseResult.success(taskService.update(task));
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        taskService.export(response);
    }

}
